const exp = require("express")

const noteRouter = exp.Router()
const verifyFn = require("../middleware/loginVerify")

const noteModel = require("../database/model/notebook")

noteRouter.get("/notebook",verifyFn,(req,res)=>{
    let username = req.session.username
    res.render("addnotebook.html",{username})
})

noteRouter.post("/noteboke",verifyFn,(req,res)=>{
    let {title,content,username} = req.body
    noteModel.create({title,content,username},(err,data)=>{
        if(!err){
            res.send("添加成功")
        }
    })
})

noteRouter.get("/deletenote",verifyFn,(req,res)=>{
    let {_id} = req.query
    console.log(_id);
    noteModel.deleteOne({_id},(err,data)=>{
        res.send("删除成功")
    })
})

noteRouter.get("/updatanote",verifyFn,(req,res)=>{
    let {_id} = req.query
    noteModel.findById({_id},(err,data)=>{
        let {content,title,username} = data
        if(!err){
            res.render("updatanote.html",{_id,content,title,username})
        }
        
    })
    
})

noteRouter.post("/updatanote",verifyFn,(req,res)=>{
    let {_id,content,title,username} = req.body
    noteModel.updateOne({_id},{content,title,username},(err,data)=>{
        if(!err){
            res.send("修改成功")
        }
    })
})


module.exports = noteRouter